--
-- Author: shunguo.chen
-- Date: 2018-03-07 16:46:25
--
-- ChessRender 星空谜棋地图node

local Armature = app:getClass("Armature")

local ClassRef = app.mvc.createRenderClass(...)

local COLOR_GOOD = cc.c3b(9,148,75)
local COLOR_BAD = cc.c3b(255,255,255)

--data = 
function ClassRef.createWithData(data, node)
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.ChessNode))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init(data)
	return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

-- ChessCell/nil
function ClassRef:init(data)
	self.super.init(self, data)

	display.setCascadeOpacityEnabled(self, true)

	local width, height = self._ui.Panel:getContentSize2()
	self._halfWidth = width / 2
	self._height = height

	self._timeline = self:getTimeline()
	self:pauseAction()

	self:refresh()
end

function ClassRef:refresh()
	local gateShow = false
	if self._data then
		local data = self._data
		local isAble = data:isTriggerAble(UD:getUid())

		self._ui.light:setVisible(isAble)
		self._ui.Panel_ami:setVisible(isAble)
		self._ui.num_1:setVisible(false)
		self._ui.num_2:setVisible(false)

		if isAble then
			self._ui.Image:loadTexture(data:getIcon())
			self._ui.light:loadTexture(data:getIconLight())
			self:playAction()

			local tp, val = data:getShowTypeValue()
			if tp == "2" or tp == "3" then
				-- 前进／退后固定格数
				local isBad = data:isBad()
				local color = isBad and COLOR_BAD or COLOR_GOOD
				if tp == "2" then
					self._ui.num_1:setTextColor(color)
					self._ui.num_1:setVisible(true)
					self._ui.num_1:setString(val)
				else
					self._ui.num_2:setTextColor(color)
					self._ui.num_2:setVisible(true)
					self._ui.num_2:setString(val)
				end
			elseif tp == "13" then
				-- buff类型
				local buffTp, buffVal = data:getShowBuffTypeValue()
				if buffTp == "1" then
					-- 下回合投掷固定点数
					local isBad = data:isBad()
					local color = isBad and COLOR_BAD or COLOR_GOOD
					self._ui.num_2:setTextColor(color)
					self._ui.num_2:setVisible(true)
					self._ui.num_2:setString(buffVal)
				end
			end

			self._ui.dot:setVisible(data:getTarget() == "1")

			gateShow = not data:isPass()
		else
			self:pauseAction()
		end
	else
		self._ui.light:setVisible(false)
		self._ui.Panel_ami:setVisible(false)
		self._ui.num_1:setVisible(false)
		self._ui.num_2:setVisible(false)
	end

	self:setGateShow(gateShow)
end

function ClassRef:getHalfWidth()
	return self._halfWidth
end

function ClassRef:setIsPath(b)
	self:setOpacity(b and 255*0.5 or 255)
end

function ClassRef:setGateShow(b)
	if b then
		if not self._gateArmature then
			local params = {
				armatureName = "guangmen",
				path = Res.ChessGateEfcPath
			}
			local armature = Armature.create(params)
			armature:setPosition(self:getPosition())
			self:addChild(armature)
			self._gateArmature = armature
		end
		self._gateArmature:playDefault(0)
	else
		if self._gateArmature then
			self._gateArmature:removeFromParent()
			self._gateArmature = nil
		end
	end
end

function ClassRef:addSelectArmature(armature)
	self:addChild(armature, -1)
end

function ClassRef:playAction()
	self._timeline:gotoFrameAndPlay(0, true)
end

function ClassRef:pauseAction()
	self._timeline:gotoFrameAndPause(0)
end

function ClassRef:setPosX(x)
	self._posX = x
end

function ClassRef:getPosX()
	return self._posX
end

function ClassRef:setTouchTip(tip, ableFunc)
	-- click tip
	display.uiAddClick(self._ui.Panel, function()
			if executeFunc(ableFunc) and not tolua.isnull(tip) 
				and self._data and self._data:isTriggerAble(UD:getUid()) then
				local worldPos = self:convertToWorldSpace(cc.p(0, 0))
				tip:setPosition(worldPos.x, worldPos.y + self._height * 0.75)
				tip:setData(self._data:getTipMsg())
				tip:show()
			end
		end)

	-- touch tip
	-- self._ui.Panel:addTouchEventListener(function(sender, eventType)
	-- 	if self._data and self._data:isTriggerAble(UD:getUid()) then
	-- 		if eventType == ccui.TouchEventType.began then
	-- 			local worldPos = self:convertToWorldSpace(cc.p(0, 0))
	-- 			tip:setPosition(worldPos.x, worldPos.y + self._height * 0.75)
	-- 			tip:setData(self._data:getTipMsg())
	-- 			tip:show()
	-- 		elseif eventType == ccui.TouchEventType.canceled or 
	-- 			eventType == ccui.TouchEventType.ended then
	-- 			tip:hide()
	-- 		end
	-- 	end
	-- end)
end

function ClassRef:setIndex(idx)
	self._idx = idx
end

function ClassRef:getIndex()
	return self._idx
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef
